#include <windows.h>
#include "resource.h"

enum {
	ID_LOADEDIT = 1,
	ID_LOADBUTTON,
	ID_SAVEEDIT,
	ID_SAVEBUTTON,
	ID_CONVERTBUTTON
};

const char g_szClassName[] = "defaultWindow";

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI showError(LPCTSTR lpText);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	WNDCLASSEX wc;
	MSG Msg;
	HWND hwnd;
	
	wc.cbSize			= sizeof(WNDCLASSEX);
	wc.style			= 0;
	wc.lpfnWndProc		= WndProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= hInstance;
	wc.hIcon			= LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground	= GetSysColorBrush(COLOR_3DFACE);
	wc.lpszMenuName		= NULL;
	wc.lpszClassName	= g_szClassName;
	wc.hIconSm			= (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
	
	if (!RegisterClassEx(&wc)) {
		showError("Window Registration Failed!");
		return 0;
	}
	
	hwnd = CreateWindowEx(0, g_szClassName, "Jedi Academy Server Log Converter", WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX   , (GetSystemMetrics(SM_CXSCREEN) - 500)/2, (GetSystemMetrics(SM_CYSCREEN) - 72)/2, 500, 100, NULL, NULL, hInstance, NULL);
	
	if (hwnd == NULL) {
		showError("Window Creation Failed!");
		return 0;
	}
	
	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);
	
	while(GetMessage(&Msg, NULL, 0, 0) > 0) {
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	
	return Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	char szFileName[MAX_PATH] = "";
	OPENFILENAME ofn;
	
    switch(msg) {
		case WM_CREATE:
			HFONT hfDefault;
			HWND hLoadEdit;
			HWND hLoadButton;
			HWND hSaveEdit;
			HWND hSaveButton;
			HWND hConvertButton;
		
			hLoadEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL, 10, 10, 300, 21, hwnd, (HMENU)ID_LOADEDIT, GetModuleHandle(NULL), NULL);
			hLoadButton = CreateWindowEx(0, "BUTTON", "Input", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 320, 10, 70, 21, hwnd, (HMENU)ID_LOADBUTTON, GetModuleHandle(NULL), NULL);
			hSaveEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL, 10, 41, 300, 21, hwnd, (HMENU)ID_SAVEEDIT, GetModuleHandle(NULL), NULL);
			hSaveButton = CreateWindowEx(0, "BUTTON", "Output", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 320, 41, 70, 21, hwnd, (HMENU)ID_SAVEBUTTON, GetModuleHandle(NULL), NULL);
			hConvertButton = CreateWindowEx(0, "BUTTON", "Convert", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 400, 10, 80, 52, hwnd, (HMENU)ID_CONVERTBUTTON, GetModuleHandle(NULL), NULL);

			hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
			SendMessage(hLoadEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
			SendMessage(hLoadButton, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
			SendMessage(hSaveEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
			SendMessage(hSaveButton, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
			SendMessage(hConvertButton, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
			break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
	        break;
        case WM_DESTROY:
            PostQuitMessage(0);
    	    break;
    	case WM_COMMAND:
			
			
			switch(wParam) {
				case ID_LOADBUTTON:
					ZeroMemory(&ofn, sizeof(ofn));

					ofn.lStructSize = sizeof(ofn);
					ofn.hwndOwner = hwnd;
					ofn.lpstrFilter = "Log Files (*.log)\0*.log\0Log Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
					ofn.lpstrFile = szFileName;
					ofn.nMaxFile = MAX_PATH;
					ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
					ofn.lpstrDefExt = "log";

					if(GetOpenFileName(&ofn)) {
						HWND hLoadEdit = GetDlgItem(hwnd, ID_LOADEDIT);
						SetWindowText(hLoadEdit, szFileName);
					}
					break;
				case ID_SAVEBUTTON:
					ZeroMemory(&ofn, sizeof(ofn));

					ofn.lStructSize = sizeof(ofn);
					ofn.hwndOwner = hwnd;
					ofn.lpstrFilter = "HTML Files (*.html)\0*.html\0RTF Files (*.rtf)\0*.rtf\0Text Files (*.txt)\0*.txt\0";
					ofn.lpstrFile = szFileName;
					ofn.nMaxFile = MAX_PATH;
					ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
					ofn.lpstrDefExt = "html";

					if(GetSaveFileName(&ofn)) {
						HWND hSaveEdit = GetDlgItem(hwnd, ID_SAVEEDIT);
						SetWindowText(hSaveEdit, szFileName);
					}
					break;
				case ID_CONVERTBUTTON:
					showError("Conversion starts here.");
					break;
			}
			break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
	}

    return 0;
}

int WINAPI showError(LPCTSTR lpText) {
	return MessageBox(NULL, lpText, "Error", MB_ICONEXCLAMATION|MB_OK);	
}
